package org.zoomdev.zoom.application;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zoomdev.zoom.common.exceptions.ZoomException;
import org.zoomdev.zoom.ioc.Ioc;
import org.zoomdev.zoom.ioc.impl.builder.IocBuilder;

public final class ZoomApplication {

    public static final String NAME = "ZOOM_APPLICATION";

    private static final Logger log = LoggerFactory.getLogger(ZoomApplication.NAME);
    private Ioc ioc;

    /**
     * 运行
     */
    public void start(String[] args) {
        try {
            log.info("===========App start ========================");
            long time = System.currentTimeMillis();
            IocBuilder builder = new IocBuilder();
            this.ioc = builder.build();
            log.info("===========App start time:[" + (System.currentTimeMillis() - time) + "]ms ========================");
        } catch (Throwable t) {
            log.error("Error when starting app", t);
            throw new ZoomException(t);
        }
    }

    public void sync() {
        if (ioc == null) {
            return;
        }

        synchronized (ioc) {
            try {
                ioc.wait();
            } catch (InterruptedException e) {

            }
        }
    }

    public Ioc getIoc() {
        return ioc;
    }

}
